
cost = ((0,1,2),(2,0,1),(1,2,0))
dep = []
computer = []

def dfs(now, pre_sum, done):
#	raw_input()
#	print dep, computer
	t_done = done.copy()
#	print "before", now, pre_sum, t_done, computer[now]
	while True:
		up = False
		for x in computer[now]:
			if x not in t_done and not dep[x] - t_done:
#				print "add", x, "dep:", dep[x]
				t_done.add(x)
				pre_sum += 1
				up = True
		if not up: break;
#	print "after", now, pre_sum, t_done, computer[now]
	if len(t_done) == n: return pre_sum 
	if t_done == done: return 1<<29
	ret = 1<<29
	for next in range(3):
		if now==next: continue
		ret = min(ret, dfs(next, pre_sum+cost[now][next], t_done))
#		print next, ret
	return ret

try:
	while True:
		ans = 1<<29
		n = input()
		computer = [set() for x in range(3)]
		for cnt, x in enumerate(raw_input().split()):
			computer[int(x)-1].add(cnt)
		dep = []
		for i in range(n):
			dep.append(set([int(x)-1 for x in raw_input().split()[1:]]))
		for i in range(3):
#			print '--------------------'
			ans = min(ans, dfs(i, 0, set()))
		print ans
except EOFError, e:
	pass
